package org.jacp.demo.ui;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.util.Map;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.Timer;
import javax.swing.border.Border;

import org.jacp.api.action.IAction;
import org.jacp.api.componentLayout.Layout;
import org.jacp.swing.rcp.component.ASwingComponent;

import com.explodingpixels.macwidgets.IAppWidgetFactory;

public class ImageEditor extends ASwingComponent {

	private JSplitPane main = null;

	private boolean lock = false;
	private int currentLimit = 200;
	private JPanel panel;
	private boolean init = true;

	/**
	 * Create the panel.
	 */
	public ImageEditor() {

	}

	@Override
	public void handleBarEntries(final Map<Layout, Container> bars) {
		final Container bottomBar = bars.get(Layout.SOUTH);
		final JButton add = new JButton(new ImageIcon(Toolkit
				.getDefaultToolkit().getImage("NSImage://NSAddTemplate")));
		add.putClientProperty("JButton.buttonType", "segmentedTextured");
		add.putClientProperty("JButton.segmentPosition", "first");
		add.setFocusable(false);
		add.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(final ActionEvent arg0) {
				lock = true;
				final int LIMIT = main.getDividerSize(); // full collapsed
				final int LIMIT_2 = 200; // full extend
				final int INTERVAL = 1;
				final boolean reverse = (main.getWidth() - main
						.getDividerLocation()) == LIMIT ? true : false;
				currentLimit = reverse ? LIMIT_2 : LIMIT;
				final Timer timer = new Timer(INTERVAL, null);
				final ActionListener listener = new ActionListener() {

					@Override
					public void actionPerformed(final ActionEvent arg0) {
						final int loc = (main.getWidth() - main
								.getDividerLocation());			
						if (!reverse) {
							if (loc > LIMIT) {
								main.setDividerLocation(main
										.getDividerLocation() + 2);
							} else {
								if(panel.isVisible())
									panel.setVisible(false);
								timer.stop();
							}
						} else {
							if(!panel.isVisible())
								panel.setVisible(true);
							if (loc < LIMIT_2) {
								main.setDividerLocation(main
										.getDividerLocation() - 2);
							} else {
								
								timer.stop();
							}
						}

					}
				};
				timer.addActionListener(listener);
				timer.start();
				lock = false;
			}
		});

		bottomBar.add(add);
	}

	@Override
	public void handleMenuEntries(final Container arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public Container handleAction(final IAction<ActionEvent, Object> action) {
		if (main == null) {

			main = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);

			final JPanel tmp = new JPanel();

			final JScrollPane scrollPane = new JScrollPane(tmp);
			IAppWidgetFactory.makeIAppScrollPane(scrollPane);
			scrollPane.setSize(new Dimension(824, 800));

			main.add(scrollPane);
			main.setBorder(null);
			panel = new JPanel();
			JLabel label = new JLabel("test");
			panel.add(label);
			main.add(panel);

			// set locations
			main.addComponentListener(new ComponentListener() {

				@Override
				public void componentShown(ComponentEvent arg0) {
					main.setDividerLocation(main.getWidth() - 200);
				}

				@Override
				public void componentResized(ComponentEvent arg0) {
					if (init) {
						main.setDividerLocation(main.getWidth() - 200);
						init = false;
					}
					if (!lock)
						main.setDividerLocation(main.getWidth() - currentLimit);
				}

				@Override
				public void componentMoved(ComponentEvent arg0) {
					// TODO Auto-generated method stub

				}

				@Override
				public void componentHidden(ComponentEvent arg0) {
					// TODO Auto-generated method stub

				}
			});
		}
		return main;
	}
}
